package com.e3.basdoc.dept.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.e3.basdoc.dept.dao.DeptDAO;
import com.e3.basdoc.dept.entity.DeptEntity;
import com.e3.basdoc.dept.model.DeptModel;
import com.e3.basdoc.dept.model.DeptQC;
import com.e3.basdoc.dept.service.DeptQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SessionContext;
import com.e3.core.base.SuperQueryService;

public class DeptQueryServiceImpl extends SuperQueryService implements DeptQueryService {

	private DeptDAO deptDAO;

	private DeptDAO getDeptDAO() {
		if (deptDAO == null) {
			deptDAO = DAOHelper.createDAOInstance(DeptDAO.class);
		}
		return deptDAO;
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptQueryService#getDeptModelById(long)
	 */
	public DeptModel getDeptModelById(long id) throws ServiceException {
		DeptEntity dept = getDeptDAO().getById(DeptEntity.class, id);
		return deptEntityToModel(dept);
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptQueryService#getDeptModelByIds(java.lang.Long)
	 */
	public List<DeptModel> getDeptModelByIds(Long... ids) throws ServiceException {
		List<DeptEntity> deptEntities = getDeptDAO().findBySingleField(DeptEntity.class, "id", ids);

		if (deptEntities != null && deptEntities.size() > 0) {
			List<DeptModel> deptModels = new ArrayList<DeptModel>(deptEntities.size());
			for (DeptEntity deptEntity : deptEntities) {
				deptModels.add(deptEntityToModel(deptEntity));
			}
			return deptModels;
		}

		return null;
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptQueryService#getDeptByCondition(com.e3.basdoc.dept.model.DeptQC)
	 */
	public List<DeptModel> getDeptByCondition(DeptQC deptQC) throws ServiceException {
		List<DeptModel> deptModels = new ArrayList<DeptModel>();
		deptQC.setCorpId(SessionContext.getCorpID());
		List<DeptEntity> depts = getDeptDAO().getDeptsByCondition(deptQC);
		if (depts != null) {
			for (DeptEntity dept : depts) {
				deptModels.add(deptEntityToModel(dept));
			}
		}
		return deptModels;
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptQueryService#getDeptChildren(com.e3.basdoc.dept.model.DeptModel)
	 */
	public List<DeptModel> getDeptChildren(DeptModel deptModel) throws ServiceException {
		if(deptModel == null){
			return null;
		}
		long pId = deptModel.getId();
		List<DeptEntity> entities = getDeptDAO().getChildren(pId, SessionContext.getCorpID());

		if (entities != null) {
			List<DeptModel> deptModels = new ArrayList<DeptModel>(entities.size());
			for (DeptEntity entity : entities) {
				DeptModel deptMd = deptEntityToModel(entity);
				deptModels.add(deptMd);
			}
			return deptModels;
		}

		return new ArrayList<DeptModel>(0);
	}

	private DeptModel deptEntityToModel(DeptEntity dept) {
		DeptModel deptModel = new DeptModel();
		deptModel = super.entityToModel(deptModel, dept);
		deptModel.setId(dept.getId());
		deptModel.setCode(dept.getCode());
		deptModel.setName(dept.getName());
//		long count = getDeptDAO().childrenCount(dept.getId(), SessionContext.getCorpID());
//		deptModel.setHasChildren(count > 0);
		deptModel.setContactAddr(dept.getContactAddress());
		deptModel.setContactPerson(dept.getContactPerson());
		deptModel.setContactWay(dept.getContactWay());
		deptModel.setEmail(dept.getEmail());
		deptModel.setFax(dept.getFax());
		deptModel.setFoundTime(new Date(dept.getFoundTime()));
		deptModel.setManager(dept.getManager());
		deptModel.setRemark(dept.getRemark());
		deptModel.setShortName(dept.getShortName());
		deptModel.setTelphone(dept.getTelphone());
		deptModel.setType(dept.getType() + "");
		deptModel.setZipcode(dept.getZipcode());
		deptModel.setOfficeAddress(dept.getOfficeAddress());
		deptModel.setParentId(dept.getParentId());
		deptModel.setRegionId(dept.getRegionId());
		deptModel.setCorpId(dept.getCorpId() + "");
		deptModel.setStatus(dept.getStatus() + "");
		return deptModel;
	}

}
